// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System;
using LABSMFController.LABS;
using LABSMFController.LABS.Classses;
using LABSMFController.LABS.Devices;
using LABSMFController.LABS.Services;
using LABSMFControllerMFCobra.Modules;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;

namespace LABSMFController.UI.Modules
{
    public class TemperatureUIModule : UIModules
    {
        private static readonly Font TempFont = Resources.GetFont(Resources.FontResources.small);


        private DispatcherTimer _update;

        public TemperatureUIModule()
        {
            Width = 200;
            Height = 200;
            Visibility = Visibility.Visible;
        }


        public override void Configure()
        {
            _update = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 0, 500)};
            _update.Tick += UpdateTick;
            _update.Start();
        }


        private void UpdateTick(object sender, EventArgs e)
        {
            Invalidate();
        }

        public override void OnRender(DrawingContext dc)
        {
            const int xTempOffset = 85;
            int y = 8;
            const int yOffset = 10;

            dc.DrawRectangle(new SolidColorBrush(Colors.White),
                             null,
                             0,
                             0,
                             Width,
                             Height
                );


            for (int c = 0; c < TemperatureServiceClass.MAXProbes; c++)
            {
                int temp = BeerProgram.GetTemp(c);
                dc.DrawText("Temp " + Location.Names[c] + " ",
                            TempFont,
                            Color.Black,
                            4, y
                    );

                dc.DrawText(
                    temp.ToString(),
                    TempFont,
                    Color.Black,
                    xTempOffset, y
                    );

                y += yOffset;
            }

            y += yOffset;

            for (int c = 0; c < BubblerServiceClass.MAXBubblers; c++)
            {
                double temp = BeerProgram.GetLevel(c);

                dc.DrawText(Location.Names[c] + " Gallons ",
                            TempFont,
                            Color.Black,
                            4, y
                    );

                dc.DrawText(
                    NumberFormatter.Format("####.##", temp),
                    TempFont,
                    Color.Black,
                    xTempOffset, y
                    );

                y += yOffset;
            }

            y += yOffset;

            for (int c = 0; c < HeaterServiceClass.MAXHeaters; c++)
            {
                dc.DrawText(BeerProgram.HeaterService.Heaters[c].Description + " ",
                            TempFont,
                            Color.Black,
                            4, y
                    );

                string on = "Is Off";
                if (BeerProgram.GetHeaterStatus(c))
                    on = "Is On";

                dc.DrawText(
                    on,
                    TempFont,
                    Color.Black,
                    xTempOffset, y
                    );
                y += yOffset;
            }

            y += yOffset;
            for (int c = 0; c < VValveDevices.MAXVValves; c++)
            {
                dc.DrawText(VValveDevices.VValves[c].Description + " ",
                            TempFont,
                            Color.Black,
                            4, y
                    );

                if (VValveDevices.VValves[c].IsThreeWay)
                {
                    dc.DrawText(
                        VValveDevices.VValves[c].AorB,
                        TempFont,
                        Color.Black,
                        xTempOffset, y
                        );
                }
                else
                {
                    double temp = BeerProgram.GetVValveSwing(c);

                    dc.DrawText(
                        NumberFormatter.Format("####.##", temp),
                        TempFont,
                        Color.Black,
                        xTempOffset, y
                        );
                }

                dc.DrawText(
                    VValveDevices.VValves[c].IsRunning.ToString(),
                    TempFont,
                    Color.Black,
                    xTempOffset + (xTempOffset/2), y
                    );

                y += yOffset;
            }
        }
    }
}